﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RayTracer;
using System.IO;
using Balloonerds.Algebra;
using System.Globalization;

namespace Demo
{
    public static class Mol
    {
        private static Dictionary<string, Atom> Atoms;

        static Mol()
        {
            Atoms = new Dictionary<string, Atom>();
            Atoms.Add("H", new Atom() { Radius = 1.20, Surface = Surface.Shiny(Color.FromWMColor(System.Windows.Media.Colors.LightGray)) });
            Atoms.Add("C", new Atom() { Radius = 1.70, Surface = Surface.Shiny(Color.FromWMColor(System.Windows.Media.Colors.White)*.10) });
            Atoms.Add("N", new Atom() { Radius = 1.55, Surface = Surface.Shiny(Color.FromWMColor(System.Windows.Media.Colors.Blue)) });
            Atoms.Add("O", new Atom() { Radius = 1.52, Surface = Surface.Shiny(Color.FromWMColor(System.Windows.Media.Colors.Crimson)) });
            Atoms.Add("F", new Atom() { Radius = 1.47, Surface = Surface.Shiny(Color.FromWMColor(System.Windows.Media.Colors.GreenYellow)) });
            Atoms.Add("P", new Atom() { Radius = 1.80, Surface = Surface.Shiny(Color.FromWMColor(System.Windows.Media.Colors.Purple)) });
            Atoms.Add("S", new Atom() { Radius = 1.80, Surface = Surface.Shiny(Color.FromWMColor(System.Windows.Media.Colors.Yellow)) });
            Atoms.Add("Cl", new Atom() { Radius = 1.75, Surface = Surface.Shiny(Color.FromWMColor(System.Windows.Media.Colors.Green)) });
            Atoms.Add("Cu", new Atom() { Radius = 1.40, Surface = Surface.Copper });
        }

        public static Union LoadMol(string fileName)
        {
            var union = new Union();

            var lines = File.ReadAllLines(fileName);

            var n = int.Parse(lines[3].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[0]);

            var boundaryRadius = 0.0;
            var center = Vector.Origin; ;
            //try
            //{
            for (var i = 0; i < n; i++)
            {
                var fields = lines[i + 4].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                var atom = Atoms[fields[3]];
                var pos = new Vector(
                    double.Parse(fields[0], NumberFormatInfo.InvariantInfo),
                    double.Parse(fields[1], NumberFormatInfo.InvariantInfo),
                    double.Parse(fields[2], NumberFormatInfo.InvariantInfo));
                center += pos;
                var radius = atom.Radius;

                boundaryRadius = Math.Max(boundaryRadius, pos.Norm + radius);

                union.Objects.Add(new Sphere()
                {
                    Center = pos,
                    Radius = radius,
                    Surface = atom.Surface
                });
            }

            //move to the geometric center
            union.Translate(Vector.Origin - (center / n));

            //}
            //catch (Exception ex)
            //{
            //}

            union.Boundary = new Sphere() { Center = Vector.Origin, Radius = boundaryRadius };

            return union;
        }


        private struct Atom
        {
            public double Radius;
            public Surface Surface;
        }
    }
}
